package com.colin.app.activity.login;

import android.support.annotation.NonNull;
import android.text.Editable;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.colin.app.R;
import com.colin.app.app.AppActivity;
import com.colin.library.help.LogHelp;
import com.colin.library.help.permission.PermissionHelp;
import com.colin.library.listener.MyTextWatcher;


public class LoginActivity extends AppActivity implements LoginContract.LoginView, View.OnClickListener {
    private LoginContract.LoginPresenter mLoginPresenter = null;
    private ImageView image_login_head;
    private ImageView image_login_delete_user;
    private ImageView image_login_password_hide;
    private ImageView image_login_delete_password;
    private EditText edit_login_user;
    private EditText edit_login_password;
    private Button button_login_ok;

    @Override
    protected void onDestroy() {
        mLoginPresenter.destroy();
        mLoginPresenter = null;
        super.onDestroy();
    }


    @Override
    public void initPresenter() {
        mLoginPresenter = new LoginPresenterImpl(this, this);
    }

    @Override
    public int initViewLayoutID() {
        return R.layout.activity_login;
    }

    @Override
    public void initView() {

        this.image_login_head = (ImageView) this.findViewById(R.id.image_login_head);
        this.image_login_delete_user = (ImageView) this.findViewById(R.id.image_login_delete_user);
        this.image_login_password_hide = (ImageView) this.findViewById(R.id.image_login_password_hide);
        this.image_login_delete_password = (ImageView) this.findViewById(R.id.image_login_delete_password);
        this.edit_login_user = (EditText) this.findViewById(R.id.edit_login_user);
        this.edit_login_password = (EditText) this.findViewById(R.id.edit_login_password);
        this.button_login_ok = (Button) this.findViewById(R.id.button_login_ok);
    }

    @Override
    public void initData() {
        mLoginPresenter.setData();
    }

    @Override
    public void initListener() {
        edit_login_user.addTextChangedListener(new MyTextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
                mLoginPresenter.showDeleteImageView(s.toString(), image_login_delete_user);
            }
        });
        edit_login_password.addTextChangedListener(new MyTextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
                mLoginPresenter.showDeleteImageView(s.toString(), image_login_delete_password);
            }
        });
        edit_login_password.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int id, KeyEvent keyEvent) {
                if (id == R.id.login || id == EditorInfo.IME_NULL) {
                    mLoginPresenter.login(edit_login_user, edit_login_password);
                    return true;
                }
                return false;
            }
        });
        this.image_login_head.setOnClickListener(this);
        this.image_login_delete_user.setOnClickListener(this);
        this.image_login_delete_password.setOnClickListener(this);
        this.image_login_password_hide.setOnClickListener(this);
        this.button_login_ok.setOnClickListener(this);

    }

    @Override
    public void initAsync() {
        //自动登录
//        mLoginPresenter.login(edit_login_user,edit_login_password);
    }

    @Override
    public void initLoginUser(String user, String password) {
        this.edit_login_user.setText(user);
        this.edit_login_password.setText(password);
        mLoginPresenter.showDeleteImageView(user, image_login_delete_user);
        mLoginPresenter.showDeleteImageView(password, image_login_delete_password);
    }

    @Override
    public boolean checkPermission(String permission) {
        return PermissionHelp.getInstance().checkPermission(this, permission);
    }

    @Override
    public void getImageHeadFromPhone() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.image_login_head:
                mLoginPresenter.getHead();
                break;
            case R.id.image_login_delete_user:
                mLoginPresenter.clearText(edit_login_user);
                break;
            case R.id.image_login_delete_password:
                mLoginPresenter.clearText(edit_login_password);
                break;
            case R.id.image_login_password_hide:
                mLoginPresenter.showPassword(image_login_password_hide, edit_login_password);
                break;
            case R.id.button_login_ok:
                mLoginPresenter.login(edit_login_user, edit_login_password);
                break;
        }
    }

    /**
     * 收到权限许可结果
     *
     * @param requestCode
     * @param permissions
     * @param grantResults
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        LogHelp.e("requestCode==" + String.valueOf(requestCode));

        LogHelp.e("grantResults==" + String.valueOf(grantResults.toString()));
        if (grantResults.length < 1) {
            LogHelp.e("权限请求失败");
        }
        for (String permission : permissions) {
            LogHelp.e("权限请求=permissions="+permission);
        }
        for (int result : grantResults) {
            LogHelp.e("权限请求=result=" + String.valueOf(result));
        }
    }
}

